//
//  AppDelegate.swift
//  MyWeibo
//
//  Created by apple on 16/4/16.
//  Copyright © 2016年 apple. All rights reserved.
//

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        // Override point for customization after application launch.
        
        //调用全局外观设置方法
        setAppearance()
        
        //判断登录效果
        
        
        //注册根控制器切换的通知
        NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("ChangeRootController:"), name: HMChangeRootVCNotification, object: nil)
        
        
        //创建window
       window = UIWindow(frame:UIScreen.mainScreen().bounds)
        
        //判断login的值  判断用户是否登录
        if HMUserAccountViewModel.sharedViewModel.login {
            
        //如果成功登录 跳转到欢迎窗口 先改成主页界面方便调试
           window?.rootViewController = HMWelcomeViewController()
        }else{
            
            //如果没有登录  跳转到主页窗口
            window?.rootViewController = HMMainViewController()
        }

        //设置背景颜色
        window?.backgroundColor = UIColor.whiteColor()

        //设置主窗口并显示
        window?.makeKeyAndVisible()
 
        return true
    }
    
    //通知方法的实现
    func ChangeRootController(noti:NSNotification){
        
        if noti.object != nil{
            
            //欢迎页发来的通知
            window?.rootViewController = HMWelcomeViewController()
            
        } else {
            //从登录控制器发来的通知
             window?.rootViewController = HMMainViewController()
        }
        
    }
    //通知的移除
    deinit{
        
        NSNotificationCenter.defaultCenter().removeObserver(self)
    }
    
    
    //设置全局外观  
    func setAppearance(){
        
        //获取全局外观代理对象
        let a = UINavigationBar.appearance()
        //统一设置全局控件文字颜色
        a.tintColor = UIColor.orangeColor()
        
    }

    func applicationWillResignActive(application: UIApplication) {
        // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
        // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
    }

    func applicationDidEnterBackground(application: UIApplication) {
        // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
        // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    }

    func applicationWillEnterForeground(application: UIApplication) {
        // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
    }

    func applicationDidBecomeActive(application: UIApplication) {
        // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    }

    func applicationWillTerminate(application: UIApplication) {
        // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    }


}

